package pl.edu.agh.mobsys.soap;

import java.util.Vector;

/**
 * SOAPBuilder handles the protocol use
 */
public class SOAPBuilder {
	private static int uid = 1;
	public static java.util.Vector<JobData> jobs = new Vector<JobData>(); // Current jobs

	// podstawowa funkcjonalnosc

	// public static String SUBSCRIBE_TEMPLATE =
	// "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'>	<SOAP-ENV:Header>		<wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>7</wsa5:MessageID>		<wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing'			SOAP-ENV:mustUnderstand='1'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:To>		<wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing'			SOAP-ENV:mustUnderstand='1'>http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/SubscribeRequest		</wsa5:Action>	</SOAP-ENV:Header>	<SOAP-ENV:Body>		<wsnt:Subscribe xmlns:wsnt='http://docs.oasis-open.org/wsn/b-2'>			<wsnt:ConsumerReference>				<wsa5:Address xmlns:wsa5='http://www.w3.org/2005/08/addressing'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:Address>			</wsnt:ConsumerReference>			<wsnt:Filter>				<wsnt:TopicExpression					xmlns:tns='http://schemas.smoa-project.com/comp/2009/01/notification/topic'					Dialect='http://docs.oasis-open.org/wsn/t-1/TopicExpression/Full'>tns://*</wsnt:TopicExpression>			</wsnt:Filter>		</wsnt:Subscribe>	</SOAP-ENV:Body></SOAP-ENV:Envelope>";
	public static String SUBSCRIBE_TEMPLATE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'>	<SOAP-ENV:Header>		<wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>uuid:7</wsa5:MessageID>		<wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing'			SOAP-ENV:mustUnderstand='1'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:To>		<wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing'			SOAP-ENV:mustUnderstand='1'>http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/SubscribeRequest		</wsa5:Action>	</SOAP-ENV:Header>	<SOAP-ENV:Body>		<wsnt:Subscribe xmlns:wsnt='http://docs.oasis-open.org/wsn/b-2'>			<wsnt:ConsumerReference>				<wsa5:Address xmlns:wsa5='http://www.w3.org/2005/08/addressing'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:Address>			</wsnt:ConsumerReference>			<wsnt:Filter>				<wsnt:TopicExpression					xmlns:tns='http://schemas.smoa-project.com/comp/2009/01/notification/topic'					Dialect='http://docs.oasis-open.org/wsn/t-1/TopicExpression/Full'>tns://*</wsnt:TopicExpression>			</wsnt:Filter>		</wsnt:Subscribe>	</SOAP-ENV:Body></SOAP-ENV:Envelope>";

	public static String RUN_JOB_TEMPLATE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'>		<SOAP-ENV:Header>		<wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>uuid:@ID@</wsa5:MessageID>		<wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing'			SOAP-ENV:mustUnderstand='1'>http://localhost:19000</wsa5:To>		<wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing'			SOAP-ENV:mustUnderstand='1'>http://schemas.ggf.org/bes/2006/08/bes-factory/CreateActivity		</wsa5:Action>	</SOAP-ENV:Header>	<SOAP-ENV:Body>		<bes-factory:CreateActivity			xmlns:bes-factory='http://schemas.ggf.org/bes/2006/08/bes-factory'>			<bes-factory:ActivityDocument>						<jsdl:JobDefinition xmlns:jsdl='http://schemas.ggf.org/jsdl/2005/11/jsdl' xmlns:jsdl-hpcpa='http://schemas.ggf.org/jsdl/2006/07/jsdl-hpcpa'>  <jsdl:JobDescription>      <jsdl:JobIdentification><jsdl:JobName>@NAME@</jsdl:JobName>      </jsdl:JobIdentification>      <jsdl:Application> <jsdl-hpcpa:HPCProfileApplication><jsdl-hpcpa:Executable>@COMMAND@</jsdl-hpcpa:Executable>  <jsdl-hpcpa:Argument>@ARGS@</jsdl-hpcpa:Argument>        </jsdl-hpcpa:HPCProfileApplication>      </jsdl:Application>  </jsdl:JobDescription></jsdl:JobDefinition>						</bes-factory:ActivityDocument>		</bes-factory:CreateActivity>	</SOAP-ENV:Body></SOAP-ENV:Envelope>";

	// public static String GET_STATUS_TEMPLATE =
	// "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'><SOAP-ENV:Header><wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>uuid:%msg_uuid%</wsa5:MessageID><wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>xmpp:smoacomp1@jabber.org/SMOAService</wsa5:To><wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>http://schemas.ggf.org/bes/2006/08/bes-factory/GetActivityStatuses</wsa5:Action></SOAP-ENV:Header><SOAP-ENV:Body><bes-factory:GetActivityStatuses xmlns:bes-factory='http://schemas.ggf.org/bes/2006/08/bes-factory'><bes-factory:ActivityIdentifier><wsa5:Address xmlns:wsa5='http://www.w3.org/2005/08/addressing'>xmpp:smoacomp1@jabber.org/SMOAService</wsa5:Address><wsa5:ReferenceParameters xmlns:wsa5='http://www.w3.org/2005/08/addressing'><smoa-comp-factory:ActivityId xmlns:smoa-comp-factory='http://schemas.smoa-project.com/comp/2009/01/factory'>@ID@</smoa-comp-factory:ActivityId></wsa5:ReferenceParameters></bes-factory:ActivityIdentifier></bes-factory:GetActivityStatuses></SOAP-ENV:Body></SOAP-ENV:Envelope>";
	public static String GET_STATUS_TEMPLATE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'><SOAP-ENV:Header><wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>uuid:@ID@</wsa5:MessageID><wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>xmpp:smoacomp1@jabber.org/SMOAService</wsa5:To><wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>http://schemas.ggf.org/bes/2006/08/bes-factory/GetActivityStatuses</wsa5:Action></SOAP-ENV:Header><SOAP-ENV:Body><bes-factory:GetActivityStatuses xmlns:bes-factory='http://schemas.ggf.org/bes/2006/08/bes-factory'><bes-factory:ActivityIdentifier><wsa5:Address xmlns:wsa5='http://www.w3.org/2005/08/addressing'>xmpp:smoacomp1@jabber.org/SMOAService</wsa5:Address><wsa5:ReferenceParameters xmlns:wsa5='http://www.w3.org/2005/08/addressing'><smoa-comp-factory:ActivityId xmlns:smoa-comp-factory='http://schemas.smoa-project.com/comp/2009/01/factory'>@ID@</smoa-comp-factory:ActivityId></wsa5:ReferenceParameters></bes-factory:ActivityIdentifier></bes-factory:GetActivityStatuses></SOAP-ENV:Body></SOAP-ENV:Envelope>";

	// rozszerzona funkcjonalnosc: przedluzenie subskrypcji, usuwanie zadania,
	// usuwanie subskrybcji
	// public static String DELETE_JOB_TEMPLATE =
	// "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'><SOAP-ENV:Header><wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>@ID@</wsa5:MessageID><wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:To><wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>http://schemas.ggf.org/bes/2006/08/bes-factory/TerminateActivites</wsa5:Action></SOAP-ENV:Header><SOAP-ENV:Body><bes-factory:TerminateActivities xmlns:bes-factory='http://schemas.ggf.org/bes/2006/08/bes-factory'><bes-factory:ActivityIdentifier><wsa5:Address xmlns:wsa5='http://www.w3.org/2005/08/addressing'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:Address><wsa5:ReferenceParameters xmlns:wsa5='http://www.w3.org/2005/08/addressing'><smoa-comp-factory:ActivityId xmlns:smoa-comp-factory='http://schemas.smoa-project.com/comp/2009/01/factory'>@ID@</smoa-comp-factory:ActivityId></wsa5:ReferenceParameters></bes-factory:ActivityIdentifier></bes-factory:TerminateActivities></SOAP-ENV:Body></SOAP-ENV:Envelope>";
	public static String DELETE_JOB_TEMPLATE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'><SOAP-ENV:Header><wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>@ID@</wsa5:MessageID><wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:To><wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>http://schemas.ggf.org/bes/2006/08/bes-factory/TerminateActivites</wsa5:Action></SOAP-ENV:Header><SOAP-ENV:Body><bes-factory:TerminateActivities xmlns:bes-factory='http://schemas.ggf.org/bes/2006/08/bes-factory'><bes-factory:ActivityIdentifier><wsa5:Address xmlns:wsa5='http://www.w3.org/2005/08/addressing'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:Address><wsa5:ReferenceParameters xmlns:wsa5='http://www.w3.org/2005/08/addressing'><smoa-comp-factory:ActivityId xmlns:smoa-comp-factory='http://schemas.smoa-project.com/comp/2009/01/factory'>@ID@</smoa-comp-factory:ActivityId></wsa5:ReferenceParameters></bes-factory:ActivityIdentifier></bes-factory:TerminateActivities></SOAP-ENV:Body></SOAP-ENV:Envelope>";
	public static String RENEW_SUBSCRIBE_TEMPLATE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'><SOAP-ENV:Header>	<wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>uuid:@SUB_ID@</wsa5:MessageID>	<wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:To>	<wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>http://docs.oasis-open.org/wsn/bw-2/SubscriptionManager/RenewRequest    </wsa5:Action>   </SOAP-ENV:Header>   <SOAP-ENV:Body><wsnt:Renew xmlns:wsnt='http://docs.oasis-open.org/wsn/b-2'>   <smn-ext:SubscriptionID xmlns:smn-ext='http://schemas.smoa-project.com/ntf/2008/12/extensions'>@SUB_ID@</smn-ext:SubscriptionID> <wsnt:TerminationTime xmlns:wsnt='http://docs.oasis-open.org/wsn/b-2'>@TERMINATION_TIME@</wsnt:TerminationTime></wsnt:Renew></SOAP-ENV:Body></SOAP-ENV:Envelope>";
	public static String DELETE_SUBSCRIBE_TEMPLATE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://www.w3.org/2003/05/soap-envelope'><SOAP-ENV:Header><wsa5:MessageID xmlns:wsa5='http://www.w3.org/2005/08/addressing'>@SUB_ID@</wsa5:MessageID><wsa5:To xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>xmpp:@USER@@@SERVER@/@RESOURCE@</wsa5:To><wsa5:Action xmlns:wsa5='http://www.w3.org/2005/08/addressing' SOAP-ENV:mustUnderstand='1'>http://docs.oasis-open.org/wsn/bw-2/SubscriptionManager/UnsubscribeRequest</wsa5:Action></SOAP-ENV:Header><SOAP-ENV:Body><wsnt:Unsubscribe xmlns:wsnt='http://docs.oasis-open.org/wsn/b-2'><smn-ext:SubscriptionID xmlns:smn-ext='http://schemas.smoa-project.com/ntf/2008/12/extensions'>@SUB_ID@</smn-ext:SubscriptionID></wsnt:Unsubscribe></SOAP-ENV:Body></SOAP-ENV:Envelope>";

	public static String getSubscribeReceiver() {
		return "smoantf1@grass2.man.poznan.pl/SMOAService";
	}

	public static String getRunJobReceiver() {
		return "smoacomp1@grass2.man.poznan.pl/SMOAService";
	}

	public static String getAskStatusReceiver() {
		return getRunJobReceiver();
	}

	// The parameter is the jobs index in the list returned by getJobs()
	public static String getAskStatusMessage(String aid/*int id*/) {
		//JobData j = jobs.elementAt(id);
		String s = GET_STATUS_TEMPLATE;
		s = replace(s, "@ID@", aid);
		return s;
	}

	public static String getSubscribeMessage(String user, String server,
			String resource) {
		String s = SUBSCRIBE_TEMPLATE;
		s = replace(s, "@USER@", user);
		s = replace(s, "@SERVER@", server);
		s = replace(s, "@RESOURCE@", resource);

		return s;
	}

	public static String getRunMessage(JobData jobData) {
		//jobData.setSendId(getId());
		jobs.addElement(jobData);
		String s = RUN_JOB_TEMPLATE;
		s = replace(s, "@NAME@", jobData.getName());
		s = replace(s, "@COMMAND@", jobData.getCommand());
		s = replace(s, "@ARGS@", jobData.getArgs());
		return s;
	}

	public static String getRenewSubscribeMessage(String user, String server,
			String resource, String subID, String terminationTime) {
		String s = RENEW_SUBSCRIBE_TEMPLATE;
		s = replace(s, "@USER@", user);
		s = replace(s, "@SERVER@", server);
		s = replace(s, "@RESOURCE@", resource);
		s = replace(s, "@SUB_ID@", subID);
		s = replace(s, "@TERMINATION_TIME@", terminationTime);

		return s;
	}

	// adres: xmpp:smoacomp1@jabber.org/SMOAService - w wersji testowej,
	// poprawic w wersji finalnej
	public static String getDeleteJobMessage(String user, String server,
			String resource, String aid) {
		//JobData j = jobs.elementAt(id);
		user="smoantf1";
		server="grass2.man.poznan.pl";
		resource="SMOAService";
		
//		user="smoatest1";
//		server="grass2.man.poznan.pl";
//		resource="JabberMix";
		
		String s = DELETE_JOB_TEMPLATE;
		s = replace(s, "@USER@", user);
		s = replace(s, "@SERVER@", server);
		s = replace(s, "@RESOURCE@", resource);
		// s = replace(s, "@USER@", "smoacomp1");
		// s = replace(s, "@SERVER@", "jabber.org");
		// s = replace(s, "@RESOURCE@", "SMOAService");
		s = replace(s, "@ID@", aid);

		return s;
	}

	public static String getDeleteSubscribeMessage(String user, String server,
			String resource, String subID) {
		String s = DELETE_SUBSCRIBE_TEMPLATE;
		s = replace(s, "@USER@", user);
		s = replace(s, "@SERVER@", server);
		s = replace(s, "@RESOURCE@", resource);
		s = replace(s, "@SUB_ID@", subID);

		return s;
	}

	private static String replace(String s, String from, String to) {
		int index = s.indexOf(from);
		while (index != -1) {
			String s1 = s.substring(0, index);
			String s2 = s.substring(index + from.length());
			s = s1 + to + s2;
			index = s.indexOf(from);
		}
		return s;
	}

	public static String[] getJobs() {
		String[] names = new String[jobs.size()];
		for (int i = 0; i < jobs.size(); i++)
			names[i] = jobs.elementAt(i).getName();
		return names;
	}

	private static String getId() {
		int i = uid;
		uid = uid + 1;
		return "uuid:" + new Integer(i).toString();
	}

//	public static JobData findJobByMsgId(String id) {
//		int s = jobs.size();
//		for (int i = 0; i < s; i++) {
//			JobData j = jobs.elementAt(i);
//			if (j.getSendId().equals(id))
//				return j;
//		}
//		return null;
//	}

	public static JobData findJob(String id) {
		int s = jobs.size();
		for (int i = 0; i < s; i++) {
			JobData j = jobs.elementAt(i);
			System.out.println("j.getAid() = " + j.getAid());
			if (j.getAid().equals(id))
				return j;
		}
		return new JobData();
	}

	public static boolean deleteJob(int id) {
		int s = jobs.size();
		JobData k = jobs.elementAt(id);
		for (int i = 0; i < s; i++) {
			JobData j = jobs.elementAt(i);
			// System.out.println("aid = " + k.getAid() + " | " +
			// "j.getAid() = " + j.getAid());
			if (j.getAid().equals(k.getAid())) {
				// System.out.println("id = " + k.getAid() + " rowne " +
				// "j.getAid() = " + j.getAid());
				jobs.removeElementAt(i);
				return true;
			}
		}
		return false;
	}

}
